package com.dragon.client.controller.auth;

import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.MenuService;
import com.dragon.repository.domain.dto.MenuDTO;
import com.dragon.repository.domain.vo.MenuVO;
import com.dragon.repository.entity.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 * @author Charlotte
 */


@RestController
@RequestMapping("menu")
@Api(tags = "菜单管理")
public class MenuController {

    @Autowired
    MenuService service;

    @GetMapping("list")
    @ApiOperation(value = "菜单列表")
    public ResponseData<List<MenuDTO>> selectMenuList(){
        List<Menu> menu = service.selectMenuList();
        return ResponseData.success(MenuDTO.convertList(menu));
    }

    @PostMapping("add")
    @ApiOperation(value="添加菜单")
    @Log
    public ResponseData addMenu(@RequestBody @Validated(Insert.class) MenuVO vo){
        return ResponseData.success(service.add(MenuVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation(value="修改菜单")
    @Log
    public ResponseData updateMenu(@RequestBody @Validated(Update.class) MenuVO vo){
        service.modify(MenuVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value="删除菜单")
    @Log
    public ResponseData delete(@ApiParam(name = "id",type = "int",required = true,value = "菜单id") @RequestParam Integer id){
        service.delete(id);
        return ResponseData.success();
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value="批量删除菜单")
    @Log
    public ResponseData deleteMenu(@ApiParam(name = "ids",type="array",required = true,value = "菜单id集合") @RequestBody List<Integer> ids){
        service.delete(ids);
        return ResponseData.success();
    }
}
